package com.xyht.sca_s.student_manage_system.modules.advanceRegistration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.RxAdvanceRegistrationPaymentItem;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.req.RxAdvanceRegistrationPaymentItemReq;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.resp.RxAdvanceRegistrationPaymentItemResp;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.mapper.RxAdvanceRegistrationPaymentItemMapper;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.service.RxAdvanceRegistrationPaymentItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 预报名缴费项目金额表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-04-10
 */
@Service
public class RxAdvanceRegistrationPaymentItemServiceImpl extends ServiceImpl<RxAdvanceRegistrationPaymentItemMapper, RxAdvanceRegistrationPaymentItem> implements RxAdvanceRegistrationPaymentItemService {

    @Resource
    private RxAdvanceRegistrationPaymentItemMapper rxAdvanceRegistrationPaymentItemMapper;

    @Override
    public ResponseResult addAdvanceRegistrationPaymentItem(RxAdvanceRegistrationPaymentItemReq addReq, String userId) {
        if (isNullOrEmpty(addReq.getPaymentName(),addReq.getPaymentAmount(),addReq.getWxPayFunctionType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<RxAdvanceRegistrationPaymentItem> paymentItemList = rxAdvanceRegistrationPaymentItemMapper.selectList(null);
        if (!paymentItemList.isEmpty()) {
            RxAdvanceRegistrationPaymentItem rxAdvanceRegistrationPaymentItem = paymentItemList.get(0);
            BeanUtils.copyProperties(addReq,rxAdvanceRegistrationPaymentItem);
            rxAdvanceRegistrationPaymentItemMapper.updateById(rxAdvanceRegistrationPaymentItem);
        }else{
            RxAdvanceRegistrationPaymentItem rxAdvanceRegistrationPaymentItem = new RxAdvanceRegistrationPaymentItem();
            BeanUtils.copyProperties(addReq,rxAdvanceRegistrationPaymentItem);
            rxAdvanceRegistrationPaymentItem.setUserId(userId);
            rxAdvanceRegistrationPaymentItemMapper.insert(rxAdvanceRegistrationPaymentItem);
        }

        return CommonResult.success();
    }

    @Override
    public ResponseResult updateAdvanceRegistrationPaymentItem(RxAdvanceRegistrationPaymentItemReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(),updateReq.getPaymentName(),updateReq.getPaymentAmount(),updateReq.getWxPayFunctionType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        RxAdvanceRegistrationPaymentItem rxAdvanceRegistrationPaymentItem = rxAdvanceRegistrationPaymentItemMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(rxAdvanceRegistrationPaymentItem)) {
            return CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_PAYMENT_ITEM_NOT_EXIST);
        }
        BeanUtils.copyProperties(updateReq,rxAdvanceRegistrationPaymentItem);
        rxAdvanceRegistrationPaymentItemMapper.updateById(rxAdvanceRegistrationPaymentItem);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getAdvanceRegistrationPaymentItemDetail() {
        RxAdvanceRegistrationPaymentItemResp paymentItemResp = new RxAdvanceRegistrationPaymentItemResp();
        List<RxAdvanceRegistrationPaymentItem> paymentItemList = rxAdvanceRegistrationPaymentItemMapper.selectList(new LambdaQueryWrapper<>());
        if (paymentItemList != null && paymentItemList.size() > 0) {
            RxAdvanceRegistrationPaymentItem registrationPaymentItem = paymentItemList.get(0);
            BeanUtils.copyProperties(registrationPaymentItem, paymentItemResp);
        }
        return CommonResult.success(paymentItemResp);
    }
}
